package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.hmall.feign.dto.PageDTO;
import com.hmall.feign.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateConfigurer;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/list")
    public PageDTO list(@RequestParam(name = "page") Integer page,
                        @RequestParam(name = "size") Integer size) {

        Page<Item> list = itemService.list(page, size);

        long total = list.getTotal();
        List<Item> records = list.getRecords();
        return new PageDTO(total, records);
    }
    @GetMapping("/list/test")
    public PageDTO list2(@RequestParam(name = "page") Integer page,
                        @RequestParam(name = "size") Integer size,
                         @RequestHeader("authorization")String id) {
        System.out.println(id);

        Page<Item> list = itemService.list(page, size);

        long total = list.getTotal();
        List<Item> records = list.getRecords();
        return new PageDTO(total, records);
    }

    @GetMapping("/{id}")
    public Item findItemById(@PathVariable("id") Long id) {
        log.info("根据id查询商品id:{}", id);
        Item item = itemService.getById(id);
        return item;
    }

    @PostMapping()
    public void saveItem(@RequestBody Item item) {
        log.info("新增商品 item:{}", item);
        itemService.save(item);
        LambdaQueryWrapper<Item> itemLambdaQueryWrapper = new LambdaQueryWrapper<>();
        itemLambdaQueryWrapper.eq(Item::getId, item.getId());
        Item one = itemService.getOne(itemLambdaQueryWrapper);
        Long id = one.getId();
        rabbitTemplate.convertAndSend("item-exchange",
                "item.add",
                id);

    }

    @PutMapping()
    public void updateItem(@RequestBody Item item) {
        log.info("修改商品 item:{}", item);
        itemService.updateById(item);

        rabbitTemplate.convertAndSend("item-exchange",
                "item.update",
                item.getId().toString());
    }

    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id,
                             @PathVariable("status") Integer status) {
        log.info("上下架 id:{}  ->  status:{}", id, status);

        itemService.update(new LambdaUpdateWrapper<Item>()
                .eq(Item::getId, id)
                .set(Item::getStatus, status));

        String routingkey = status == 1 ? "item.up" : "item.down";
        rabbitTemplate.convertAndSend("item-exchange",
                routingkey,
                id.toString());

    }

    @DeleteMapping("/{id}")
    public void deleteItem(@PathVariable("id") Long id) {
        log.info("删除id:{}", id);
        itemService.removeById(id);

        rabbitTemplate.convertAndSend("item-exchange",
                "item.delete",
                id.toString());

    }

    @PostMapping("/restoreStock/{itemId}/{num}")
    public void restoreStock(@PathVariable(name = "itemId")Long itemId
            ,@PathVariable(name = "num")Integer num){
        log.info("恢复商品库存 itemId:{}  num:{}", itemId, num);
        Item item = itemService.getById(itemId);
        itemService.update(new LambdaUpdateWrapper<Item>()
                .eq(Item::getId, itemId)
                .set(Item::getStock,item.getStock()+ num));

    }

    @PostMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable(name = "itemId")Long itemId
            ,@PathVariable(name = "num")Integer num){
        log.info("修改商品库存 itemId:{}  num:{}", itemId, num);
        Item item = itemService.getById(itemId);
        itemService.update(new LambdaUpdateWrapper<Item>()
                .eq(Item::getId, itemId)
                .set(Item::getStock,item.getStock()+ num));
    }

}
